(function($){
    $.fn.clickSort = function(opts){
        var defaults = {
            speed:200
        }
        var option = $.extend(defaults,opts);
        this.each(function(){
            var _this = $(this);
            _this.on('click','.moveup',function(){
                // var parent = $(this).parents('.sortableitem');
                var parent = $(this).parents('[sortableitem]');
                console.log(parent.html())
                // var prevItem = parent.prev('.sortableitem');
                var prevItem = parent.prev('[sortableitem]');
                console.log(prevItem.html())
                if(prevItem.length==0)return;
                var parentTop = parent.position().top;
                var prevItemTop = prevItem.position().top;
                parent.css('visibility','hidden');
                prevItem.css('visibility','hidden');
                parent.animate({top:prevItemTop},option.speed,function(){
                    // $(this).remove();
                    parent.insertBefore(prevItem).css('visibility','visible');
                    console.log(parent.html());
                    option.callback(parent,prevItem);
                });
                prevItem.animate({top:parentTop},option.speed,function(){
                    // $(this).remove();
                    prevItem.css('visibility','visible');
                });
            });
            _this.on('click','.movedown',function(){
                var _obj = $(this);
                // var parent = $(this).parents('.sortableitem');
                var parent = $(this).parents('[sortableitem]');

                // var nextItem = parent.next('.sortableitem');
                var nextItem = parent.next('[sortableitem]');
                if(nextItem.length==0)return;
                var parentTop = parent.position().top;
                var nextItemTop = nextItem.position().top;
                parent.css('visibility','hidden');
                nextItem.css('visibility','hidden');
                parent.animate({top:nextItemTop},option.speed,function(){
                    parent.insertAfter(nextItem).css('visibility','visible');
                    option.callback(parent,nextItem);
                });
                nextItem.animate({top:parentTop},option.speed,function(){
                    nextItem.css('visibility','visible');
                });
            });

        });
    }
})(jQuery)